import Vue from 'vue'
// 引入vuex
import Vuex from 'vuex'
Vue.use(Vuex)

const store = new Vuex.Store({
  state: {
    list: [
      {
        name: "诸葛亮",
        price: 1000,
        num: 1,
        checked: false,
      },
      {
        name: "蔡文姬",
        price: 1500,
        num: 1,
        checked: false,
      },
      {
        name: "妲己",
        price: 2000,
        num: 1,
        checked: false,
      },
      {
        name: "鲁班",
        price: 2200,
        num: 1,
        checked: false,
      },
    ],
  },
  mutations: {
    delFn(state,payload) {
      state.list= state.list.filter(item => item.name !== payload)
    },
    addFn(state,payload) {
      const obj = state.list.find(item => item.name === payload)
      obj.num++
    },
    subFn(state,payload) {
      const obj = state.list.find(item => item.name === payload)
      if(obj.num === 0) return
      obj.num--
    },
  },
  getters: {
    total(state) {
      return state.list.reduce((sum,item)=> sum + item.num*item.price,0)
    }
  }
})
export default store
